from django.urls import reverse
from rest_framework.test import APIClient
from django.db.models import Sum
from play.models import Play
from video.models import Videos
from django.test import TestCase
from user.models import UserProfile


class TestPlayViewSet(TestCase):

    def setUp(self):
        self.client = APIClient()
        self.user1 = UserProfile.objects.create_user(id=1, username='testuser1', password='123456456')
        self.user2 = UserProfile.objects.create_user(id=2, username='testuser2', password='123456456')
        self.user3 = UserProfile.objects.create_user(id=3, username='testuser3', password='123456456')
        self.video1 = Videos.objects.create(vid=1, title='test1', duration=10, mup=self.user1, type='1')
        self.video2 = Videos.objects.create(vid=2, title='test2', duration=10, mup=self.user1, type='2')
        self.video3 = Videos.objects.create(vid=3, title='test3', duration=10, mup=self.user1, type='2')
        self.play1 = Play.objects.create(pid=1, video=self.video1, count=6, user=self.user1)
        self.play2 = Play.objects.create(pid=2, video=self.video1, count=55, user=self.user2)
        self.play3 = Play.objects.create(pid=3, video=self.video2, count=10, user=self.user3)

    def test_get_play_count_ok(self):
        url = reverse('play-count', kwargs={'pk': '1'})
        response = self.client.get(url)

        assert response.status_code == 200
        data = response.json()
        assert data['play_count'] == 61

    def test_get_play_count_not_exist(self):
        url = reverse('play-count', kwargs={'pk': '3'})
        response = self.client.get(url)

        assert response.status_code == 200
        data = response.json()
        self.assertIsNone(data['play_count'])

    def test_get_play_history(self):
        url = reverse('play-history', kwargs={'user_id': 1, 'num': 2, 'from': 0})
        response = self.client.get(url)
        data = response.json()['plays']
        self.assertEqual(len(data), 1)

    def test_get_play_history_not_exist(self):
        url = reverse('play-history', kwargs={'user_id': 0, 'num': 2, 'from': 0})
        response = self.client.get(url)
        data = response.json()['plays']
        self.assertEqual(len(data), 0)

    def test_get_recommendations(self):
        url = reverse('recommend')
        response = self.client.get(url)
        data = response.json()['recommendations']
        self.assertEqual(data[0]['video_id'], '1')

    def test_get_top_videos_by_type(self):
        url = reverse('toprecommend')
        response = self.client.get(url)
        data = response.json()['top_videos']
        self.assertEqual(len(data), 2)

    def test_search_videos_exsit(self):
        url = reverse('searchVideo')
        response = self.client.get(url, {'title': 'test1', 'page': 0})
        data = response.json()['videos']
        self.assertEqual(len(data), 1)

    def test_search_videos_not_exsit(self):
        url = reverse('searchVideo')
        response = self.client.get(url, {'title': 'test4', 'page': 0})
        data = response.json()['total']
        self.assertEqual(data, 0)







